package top.huhuiyu.springboot2.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.huhuiyu.springboot2.dao.TbNewsMapper;
import top.huhuiyu.springboot2.entity.*;
import top.huhuiyu.springboot2.service.TbNewsService;

import java.util.List;

@Service
public class TbNewsServiceImpl implements TbNewsService {
  @Autowired
  private TbNewsMapper tbNewsMapper;

  @Override
  public BaseResult delete(Integer id) {
    tbNewsMapper.delete(id);
    BaseResult result = new BaseResult();
    result.setSuccess(true);
    result.setCode(200);
    result.setMessage("删除成功");
    return result;
  }

  @Override
  public BaseResult update(TbNews tbNews) {
    int i = tbNewsMapper.update(tbNews);
    if (i == 1) {
      return BaseResult.ok("修改成功");
    }else {
      return BaseResult.fail("修改失败");
    }
//    return i==1?BaseResult.ok("修改成功"):BaseResult.fail("修改失败");
//    BaseResult result = new BaseResult();
//    if (i == 1) {
//      result.setSuccess(true);
//      result.setCode(200);
//      result.setMessage("修改成功");
//    } else {
//      result.setSuccess(false);
//      result.setCode(500);
//      result.setMessage("修改失败");
//    }
//    return result;
  }


  @Override
  public BaseDataResult<TbNews> queryByKey(Integer id) {
    TbNews news = tbNewsMapper.queryByKey(id);
//    BaseDataResult<TbNews> result = new BaseDataResult<>();
    if (news == null) {
//      result.setMessage("没有相关数据");
//      result.setSuccess(false);
//      result.setCode(500);
      return BaseDataResult.fail("没有相关数据", TbNews.class);
    } else {
      // 查询成功要修改点击量
      tbNewsMapper.updateHits(id);
//      result.setData(news);
//      result.setSuccess(true);
//      result.setCode(200);
      return BaseDataResult.ok(news);
    }
//    return result;
  }


  @Override
  public BaseDataResult<PageResult<List<TbNews>>> query(NewsQuery query, PageBean page) {
    // 在查询方法的前面调用开始分页的语句，参数是页码和分页大小
    PageHelper.startPage(page.getPageNumber(), page.getPageSize());
    // 接下来的查询会被分页的插件转换成分页查询的效果
    // 第一会自动执行记录的查询语句
    // 第二会将查询记录的语句修改成带limit的分页查询语句
    Page<TbNews> list = (Page<TbNews>) tbNewsMapper.queryAll(query);
    // 转换的查询结果可以获取到分页的相关信息，比如记录总数getTotal()
    page.setTotal(list.getTotal());
    BaseDataResult<PageResult<List<TbNews>>> result = new BaseDataResult<>();
    // 转换的查询结果还包括分页查询的记录集getResult()
    result.setData(new PageResult<>(list.getResult(), page));
    result.setSuccess(true);
    result.setCode(200);
    result.setMessage("");
    return result;
  }

  @Override
  public BaseResult add(TbNews tbNews) {
    int i = tbNewsMapper.add(tbNews);
    BaseResult result = new BaseResult();
    if (i == 1) {
      result.setSuccess(true);
      result.setCode(200);
      result.setMessage("添加成功");
    } else {
      result.setSuccess(false);
      result.setCode(500);
      result.setMessage("添加失败");
    }
    return result;
  }


}
